/*
Copyright 2010,2011 Matt Van Der Westhuizen

This file is part of CGLL.

CGLL is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

CGLL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with CGLL.  If not, see <http://www.gnu.org/licenses/>.
*/

package org.chaoticengine.cgll.entity.component.tween;

import org.chaoticengine.cgll.entity.Entity;

/**
 *
 * @author Matt Van Der Westhuizen <mattpwest@gmail.com>
 */
public class AlphaTween extends Tween {
    protected float sAlpha = 0.0f;
    protected float eAlpha = 0.0f;

    public AlphaTween(float startAlpha, float endAlpha, int duration) {
        super(duration);
        this.sAlpha = startAlpha;
        this.eAlpha = endAlpha;
    }

    @Override
    public void doUpdate(int delta) {
        if (repeat.equals(RepeatMode.None)) {
            // Do nothing
        } else if (repeat.equals(RepeatMode.Loop)) {
            // Restart
            if (timePassed >= duration) {
                timePassed = 0;
            }
        } else if (repeat.equals(RepeatMode.PingPong)) {
            // Swop start & end values and restart
            if (timePassed >= duration) {
                timePassed = 0;

                float f = sAlpha;
                sAlpha = eAlpha;
                eAlpha = f;
            }
        }

        float a = sAlpha + (eAlpha - sAlpha) * (timePassed / (float) duration);
        if (a < 0.0f) {
            a = 0.0f;
        } else if (a > 1.0f) {
            a = 1.0f;
        }
        
        changeAlpha(a);
    }

    protected void changeAlpha(float alpha) {
        Entity e = getOwner().getOwner();
        if (e != null) {
            e.setAlpha(alpha);
        }
    }
}
